/**
 * DataEntry.java
 * @author Jasmine Jones
 * 
 * This UI class is the FORM to create a USER
 */
package ocrs.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.SimpleRadioButton;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class DataEntry extends Composite {

	private TextBox nameTextBox;
	private TextBox emailTextBox; 
	private TextBox addressTextBox; 
	private PasswordTextBox passwordTextBox;
	
	/**
	 * DataEntry
	 * Define widget to be displayed
	 */
	public DataEntry() {
		
		FormPanel formPanel = new FormPanel();
		initWidget(formPanel);
		formPanel.setWidth("246px");
		
		FlexTable entryFlexTable = new FlexTable();
		formPanel.setWidget(entryFlexTable);
		entryFlexTable.setSize("100%", "100%");
		
		//Text Entry
		Label lblCreateNewUser = new Label("CREATE NEW USER (enter info below)");
		entryFlexTable.setWidget(0, 0, lblCreateNewUser);
		
		Label lblName = new Label("Name");
		entryFlexTable.setWidget(1, 0, lblName);
		
		nameTextBox = new TextBox();
		nameTextBox.setVisibleLength(30);
		entryFlexTable.setWidget(2, 0, nameTextBox);
		
		Label emailLabel = new Label("Email (userid@umbc.edu)");
		entryFlexTable.setWidget(3, 0, emailLabel);
		
		emailTextBox = new TextBox();
		emailTextBox.setVisibleLength(30);
		entryFlexTable.setWidget(4, 0, emailTextBox);
		
		Label addressLabel = new Label("Street Address");
		entryFlexTable.setWidget(5, 0, addressLabel);
		
		addressTextBox = new TextBox();
		addressTextBox.setVisibleLength(30);
		entryFlexTable.setWidget(6, 0, addressTextBox);
		
		Label passwordLabel = new Label("Account Password");
		entryFlexTable.setWidget(7, 0, passwordLabel);
		
		passwordTextBox = new PasswordTextBox();
		passwordTextBox.setVisibleLength(30);
		entryFlexTable.setWidget(8, 0, passwordTextBox);
		
		Label permissLabel = new Label("Permission Level (choose only one)");
		entryFlexTable.setWidget(9, 0, permissLabel);
		
		// toggle grid
		Grid grid = new Grid(4, 2);
		entryFlexTable.setWidget(10, 0, grid);
		
		Label studentButtonLbl = new Label("Student");
		grid.setWidget(0, 0, studentButtonLbl);
		
		Label instrButtonLbl = new Label(" Instructor");
		grid.setWidget(1, 0, instrButtonLbl);
		
		Label adminLbl = new Label("Administrator (Academic)");
		grid.setWidget(2, 0, adminLbl);
		
		Label itAdminLbl = new Label("Administrator (IT)");
		grid.setWidget(3, 0, itAdminLbl);
		
		RadioButton studentToggle = new RadioButton(" studentToggle", "Student");
		studentToggle.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				
			}
		});
		grid.setWidget(0, 1, studentToggle);
		
		RadioButton instructorToggle = new RadioButton("Instructor", "Instructor");
		grid.setWidget(1, 1, instructorToggle);
		
		RadioButton schoolAdminToggle = new RadioButton("Admin (Academic)", "Admin (Academic)");
		grid.setWidget(2, 1, schoolAdminToggle);
		
		RadioButton itAdminToggle = new RadioButton("Admin (IT)", "Admin (IT)");
		grid.setWidget(3, 1, itAdminToggle);
		
		grid.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_CENTER);
		
		Button dataSubmitButton = new Button("Submit");
		entryFlexTable.setWidget(11, 0, dataSubmitButton);
	}
	
	/**
	 * aggregateData
	 * 
	 * Create User from information and pass to Presenter
	 */
	public void aggregateData( ){
		//get text together and make sure its valid
		//TODO add actual validator
		String name = nameTextBox.getText();
		String address = addressTextBox.getText();
		String email = emailTextBox.getText();
		String password = passwordTextBox.getText();
		
		//if text fields is empty give empty string. TBreplaced!!
		name = replaceIfEmpty(name);
		address = replaceIfEmpty(address);
		email = replaceIfEmpty(email);
		password = replaceIfEmpty(password);
		
		//make new user
		//Presenter.
	}

	/**
	 * (private) replaceIfEmpty
	 * @param str
	 * @return empty string if passes string is null
	 */
	private String replaceIfEmpty(String str)
	{
		return (str == null ? "": str);
	}
}
